package models;

import java.sql.Date;
import java.util.*;
import javax.persistence.*;
import javax.validation.Constraint;

import play.api.mvc.Session;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;

@Entity
public class edu_users extends Model{

	@Id
	public int user_id;
	
	public String name;
	
	public String surname;
	
	public String email;
	
	@Constraints.Required
	public String password;

	public String Gender;
	
	public Date birth_date;
	
	public String picture;
	
	public String web_site;
	
	public Date registration_date;
	
	public String city;
	
	public String country;
	
	public int is_active;
	
	public int user_type;	
	
	public String user_device_id;

	
	public static Finder<Long,edu_users> find = new Finder<Long,edu_users>(Long.class, edu_users.class); 
		
	
	public static edu_users findByEmail(String email) {
        return edu_users.find.where().like("email", email).findUnique();
    }
	
	public static edu_users findById(int Id) {
        return edu_users.find.where().eq("user_id", Id).findUnique();
    }
	
	public static edu_users validateUser(String email,String password)
	{
		edu_users eu=edu_users.findByEmail(email);
		
		//return (eu.email.equals(email) && eu.password.equals(password));
		return eu;
	
	}
	
	public static edu_users getCurrentUser(){
		if(play.mvc.Controller.session().containsKey("email")){
			return findByEmail(play.mvc.Controller.session().get("email"));
		}
		else
			return null;
	}
	
	public static boolean changeActiveStatusById(int user_id, boolean is_active)
	{
		try
		{
			edu_users eu = edu_users.findById(user_id);
			if (eu.user_id > 0 )
			{	
				if (is_active == true)
					eu.is_active = 1;
				else
					eu.is_active = 0;
				eu.save();				
			}
			return true;
		}
		catch (Exception ex)
		{
			return false;
		}
	}

	public static boolean getUserByDeviceId(String user_device_id){
		edu_users eu= edu_users.find.where().eq("user_device_id", user_device_id).findUnique();
		
		if(eu!=null && eu.user_id>0)
			return true;
		else
			return false;
	}

	public static boolean validateUserForMobile(String email,String password,String user_device_id){
		edu_users eu=edu_users.findByEmail(email);
		
		if(eu.email.equals(email) && eu.password.equals(password)){
			eu.user_device_id=user_device_id;
			eu.save();
			return true;
		}
		else
			return false;
	}
}
